package com.keyinong.caizhengting.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.keyinong.caizhengting.adapter.XiaotieshiAdapter;
import com.keyinong.caizhengting.entity.XiaotieshiEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author：郑谦
 * @时间：2017-4-10 下午4:02:27
 * @类名：XiaotieshiActivity.java
 * @功能: 小贴士列表
 */

public class XiaotieshiActivity extends Activity {

    ArrayList<XiaotieshiEntity> xiaotieshilist;
    HttpTools httptools = null;
    private XiaotieshiAdapter xiaotieshiadapter;
    private ImageButton ib_back;
    private PullToRefreshListView pull_dingdan;
    private int offset = 0;
    private int page = 10;
    private String token;
    private OnItemClickListener itemclicke = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position,
                                long id) {
            Intent intent = new Intent();
            intent.setClass(XiaotieshiActivity.this, NewxiaotieshiActivity.class);
            intent.putExtra("id", xiaotieshilist.get(position - 1).getId());
            startActivity(intent);

        }
    };
    /**
     * 上下拉刷新
     */
    private OnRefreshListener2<ListView> onrefresh = new OnRefreshListener2<ListView>() {

        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            offset = 0;
            xiaotieshilist.clear();
            httptools.getArticles(token, "" + page, offset + "", new JsonXiaotieshi());
            if (NetWorkUtil.isNetwork(XiaotieshiActivity.this) == false) {
                //				MyToast.mytoast(getActivity(), "当前网络不可用");
                Toast.makeText(XiaotieshiActivity.this, "当前网络不可用", Toast.LENGTH_SHORT).show();
                pull_dingdan.onRefreshComplete();
                return;
            }

        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            offset += page;
            httptools.getArticles(token, "" + page, offset + "", new JsonXiaotieshi());
            //			setSection();
            //			adapter.notifyDataSetChanged();
            if (NetWorkUtil.isNetwork(XiaotieshiActivity.this) == false) {
                Toast.makeText(XiaotieshiActivity.this, "当前网络不可用", Toast.LENGTH_SHORT).show();
                pull_dingdan.onRefreshComplete();
                return;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_xiaotieshi);
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        token = sp.getString("data", "");
        pull_dingdan = (PullToRefreshListView) findViewById(R.id.pull_dingdan);
        pull_dingdan.setMode(Mode.BOTH);
        pull_dingdan.setOnRefreshListener(onrefresh);
        pull_dingdan.setOnItemClickListener(itemclicke);
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        ib_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Xiaotieshi();
    }

    /**
     * 获取小贴士列表
     */
    private void Xiaotieshi() {
        if (NetWorkUtil.isNetwork(getApplicationContext()) == false) {
            Toast.makeText(getApplicationContext(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
        xiaotieshilist = new ArrayList<XiaotieshiEntity>();

        //		Log.i("shi", "token---" + token);
        if (httptools == null) {
            httptools = new HttpTools(XiaotieshiActivity.this);
        }
        httptools.getArticles(token, "" + page, offset + "", new JsonXiaotieshi());

    }

    public class JsonXiaotieshi extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                Log.i("info", response.toString());
                if (code == true) {
                    JSONArray data = response.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject object = data.getJSONObject(i);
                        String id = object.getString("id");
                        String pic = object.getString("pic");
                        String brief = object.getString("brief");
                        String show_time = object.getString("show_time");

                        xiaotieshilist.add(new XiaotieshiEntity(id, pic, brief, show_time));
                    }
                }
                xiaotieshiadapter = new XiaotieshiAdapter(XiaotieshiActivity.this, xiaotieshilist);
                pull_dingdan.setAdapter(xiaotieshiadapter);
                pull_dingdan.getRefreshableView().setSelection(offset);
                pull_dingdan.setDividerDrawable(null);
                //				Utility.setGridViewHeightBasedOnChildren(pull_twosort);
                pull_dingdan.onRefreshComplete();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
